<template>
  <div>
    <el-card>
      <el-table border stripe style="width: 100%" :data="skuList">
        <el-table-column type="index" label="序号" width="80" align="center">
        </el-table-column>
        <el-table-column prop="skuName" label="名称" width="width">
        </el-table-column>
        <el-table-column prop="skuDesc" label="描述"> </el-table-column>
        <el-table-column prop="address" label="默认图片">
          <template slot-scope="{ row }">
            <img :src="row.skuDefaultImg" style="width: 100px; height: 100px" />
          </template>
        </el-table-column>
        <el-table-column prop="weight" label="重量(KG)"> </el-table-column>
        <el-table-column prop="price" label="价格(元)"> </el-table-column>
        <el-table-column label="操作" width="350">
          <template slot-scope="{ row }">
            <hint-button
              type="success"
              icon="el-icon-bottom"
              size="mini"
              title="下架sku"
              @click="cancelSale(row)"
              v-if="row.isSale == 1"
            ></hint-button>
            <hint-button
              type="success"
              icon="el-icon-top"
              size="mini"
              title="上架sku"
              v-else
              @click="onSale(row)"
            ></hint-button>
            <hint-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              title="修改sku"
            ></hint-button>
            <hint-button
              type="info"
              icon="el-icon-info"
              size="mini"
              title="查看详情"
              @click="openDrawer(row)"
            ></hint-button>
            <hint-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              title="删除sku"
            ></hint-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        style="margin-top: 20px; text-align: center"
        :current-page="page"
        @current-change="getSpuList"
        @size-change="handleSizeChange"
        :page-size="limit"
        :page-sizes="[3, 5, 10]"
        layout="prev, pager, next,jumper,->,total,sizes "
        :total="total"
      >
      </el-pagination>
    </el-card>
    <el-drawer
      :visible.sync="drawer"
      :with-header="false"
      size="50%"
      :before-close="beforeClose"
    >
      <el-form label-width="80px" style="margin-left: 50px">
        <el-form-item label="名称">{{ skuById.skuName }}</el-form-item>
        <el-form-item label="描述">{{ skuById.skuDesc }}</el-form-item>
        <el-form-item label="价格">{{ skuById.price }}</el-form-item>
        <el-form-item label="平台属性">
          <el-tag
            style="margin-left: 20px"
            type="success"
            v-for="attr in skuById.skuAttrValueList"
            :key="attr.id"
            >{{ attr.attrName }}-{{ attr.valueName }}</el-tag
          >
        </el-form-item>
        <el-form-item label="商品图片">
          <div class="block">
            <el-carousel height="150px">
              <el-carousel-item
                v-for="imgSrc in skuById.skuImageList"
                :key="imgSrc.id"
              >
                <img :src="imgSrc.imgUrl" />
              </el-carousel-item>
            </el-carousel>
          </div>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
// 引入swiper
import Swiper from "swiper";
export default {
  name: "Sku",
  data() {
    return {
      page: 1,
      limit: 5,
      total: 20,
      skuList: [],
      drawer: false,
      skuById: {},
    };
  },
  mounted() {
    this.getSpuList();
  },
  methods: {
    // 获取sku列表数据
    async getSpuList(pager = 1) {
      this.page = pager;
      const { page, limit } = this;
      let result = await this.$API.sku.reqGetSkuList(page, limit);
      if (result.code == 200) {
        this.skuList = result.data.records;
        this.total = result.data.total;
      }
    },
    // 变更每页展示数量的回调
    handleSizeChange(limit) {
      this.limit = limit;
      this.getSpuList(this.page);
    },
    // 下架按钮
    async cancelSale(row) {
      let result = await this.$API.sku.reqCancel(row.id);
      if (result.code == 200) {
        this.getSpuList(this.page);
        this.$message({
          message: "下架成功",
          type: "success",
        });
      }
    },
    // 上架操作
    async onSale(row) {
      let result = await this.$API.sku.reqSale(row.id);
      if (result.code == 200) {
        this.getSpuList(this.page);
        this.$message({
          message: "上架成功",
          type: "success",
        });
      }
    },
    // 产看详情
    async openDrawer(row) {
      this.drawer = true;
      let result = await this.$API.sku.reqSkuById(row.id);
      if (result.code == 200) {
        this.skuById = result.data;
      }
    },
    // 抽屉关闭前的回调
    beforeClose(done) {
      this.skuById = {};
      done();
    },
  },
  watch: {
    skuById: {
      immediate: true,
      handler() {
        this.$nextTick(() => {
          var mySwiper = new Swiper(this.$refs.swiper, {
            mousewheel: true,
            loop: true, // 循环模式选项
            // 如果需要分页器
            pagination: {
              el: ".swiper-pagination",
              clickable: true,
            },
          });
        });
      },
    },
  },
};
</script>

<style>
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
.el-carousel__button{
  width: 10px;
  height: 10px;
  background: pink;
  border-radius: 50%;
}
</style>
